var app = getApp()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    auth: true,
    number: 2,//奖项数量 
    user1: [//一等奖获奖状况
      {
        pic: "/static/2.jpg",
        name: "李坤",
      },
    ],
    user2: [//二等奖获奖状况
      {
        pic: "/static/2.jpg",
        name: "李坤",
      },

    ],
    user3: [//三等奖获奖状况
      {
        pic: "/static/2.jpg",
        name: "李坤",
      }
    ],
    a: true,
    b:false,
    name: '',
    pic: '',
    height_screen: 0,
    top_height: 0,
    background: 'rgba(255, 255, 255, 1)',
    color: 'rgba(0, 0, 0, 1)',
    titleText: '导航栏',
    titleImg: '',
    backIcon: '',
    homeIcon: '',
    fontSize: 16,
    iconHeight: 19,
    iconWidth: 58,
    could_join: true,
    share_flag: false,
    join: false,
    cd: 7,
    canyu: ["/images/icn-zu@3x.png",
      "/images/icn-zu@3x.png",
      "/images/icn-zu@3x.png",
      "/images/icn-zu@3x.png",
      "/images/icn-zu@3x.png",
      "/images/icn-zu@3x.png",
      "/images/icn-zu@3x.png",
    ],
    level: '',//几等奖
    state: '', //个人对于奖项的状态
    awardid: '',
    imgurls: [],
    jpms: '',
    index: 1, //抽奖方式
    jpname: '',
    jpnum: 0,
    animation: '',
    date: '', //日期
    s: 1,
    status: 0, //奖项状态
    kpnum: 0, //开奖人数||最多抽奖人数
    userid: '', //用户登录
    navHeight: 0,
    prizes: [],
    luck: false
  },
  whole: function () {
    wx.navigateTo({
      url: '/pages/chakanrenshu/chakanrenshu?awardid=' + this.data.awardid,
    })
  },

  onQuery: function(){
    const db = wx.cloud.database()
    db.collection('lottery').doc(this.data.awardid)
    .get().then(res => {
      console.log('[数据库] [查询记录] 成功: ', res)
      let lottery = res.data;
      let pic = [];
      pic.push(lottery.pic1);
      this.setData({
        data_lottery: lottery,
        pic,
        index: lottery.way,
        kpnum: lottery.num,
        cd: lottery.total,
        status: lottery.status,
        state: lottery.state
      });
    })
  },
  onQueryRecord: function(){
    const db = wx.cloud.database()
    db.collection('record')
    .where({
      lottery:this.data.awardid,
    })
    .limit(10)
    .get().then(res => {
      console.log('[数据库] [查询记录] 成功: ', res)
      let records = res.data;
      let canyu = [];
      records.forEach(element => {
        canyu.push(element.userInfo.avatarUrl);
      });
      this.setData({
        canyu
      });
    })
  },
  onQueryPrize: function(){
    const db = wx.cloud.database()
    db.collection('prize')
    .where({
      lottery:this.data.awardid
    })
    .get().then(res => {
      console.log('[数据库] [查询记录] 成功: ', res)
      let prizes = res.data;
      let arr = [];
      prizes.forEach(prize => {
        arr.push(prize._openid);
      })
      let luck = false;
      if(arr.indexOf(app.globalData.openid) != -1){
        luck = true;
      }
      this.setData({
        prizes,
        luck
      });
    })
  },


/**
* 生命周期函数--监听页面加载
*/
  onLoad: function (options) {
    this.setNavSize()
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight)
        that.setData({
          height_screen: res.windowHeight
        })
      }
    })
    this.setData({
      top_height: app.globalData.top_height
    })
    this.attached()
    wx.hideShareMenu();
    //options.awardid
    var source = options.source;
    var awardid = options.awardid;
    var userid = app.globalData.userid;

    that.setData({
      source,
      awardid: awardid
    },()=>{
      this.onQuery();
      this.onQueryRecord();
      this.onQueryPrize();
    })


  },


  // 通过获取系统信息计算导航栏高度
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      statusHeight: statusHeight,
      navHeight: navHeight
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.animation = wx.createAnimation();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      auth: app.globalData.auth
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    var awardid = this.data.awardid;
    var userid = app.globalData.userid;
    var that = this;
    console.log(awardid, userid);


  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    console.log('data:', that.data)
    return {
      // title: '转发',
      path: '/pages/index/index',
      //path: '/pages/awardconfirm/awardconfirm?awardid=' + that.data.awardid,
      title: that.data.name + '邀你参与[' + that.data.data_lottery.name1 + '等]抽奖！',
      imageUrl: that.data.imgurls[0],

      success: function (res) {
        wx.showToast({
          title: '已转发',
        })
      }

    }
  },
  // 返回事件

  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      statusHeight: statusHeight,
      navHeight: navHeight
    })
  },


  setStyle: function () {
    var that = this,
      containerStyle, textStyle, iconStyle;
    containerStyle = [
      'background:' + that.data.background
    ].join(';');
    textStyle = [
      'color:' + that.data.color,
      'font-size:' + that.data.fontSize + 'px'
    ].join(';');
    iconStyle = [
      'width: ' + that.data.iconWidth + 'px',
      'height: ' + that.data.iconHeight + 'px'
    ].join(';');
    that.setData({
      containerStyle: containerStyle,
      textStyle: textStyle,
      iconStyle: iconStyle
    })
  },

  attached: function () {
    var that = this;
    that.setNavSize();
    that.setStyle();
  },

  join: function () {
    var that = this
    var state = that.data.state;
    console.log(state);

    


  },

  handopen: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '你确定你要开奖了？',
      confirmText: '确认开奖',
      cancelText: '我再看看',
      confirmColor: 'darkred',
      cancelColor: 'darkgray',
      success(res) {
        if (res.confirm) {

 

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  go_to_lotteryCreate: function () {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  share_lottery: function (res) {
    if (this.data.awardid) {
      if (res.from === 'button') {
      }
      return {
        title: '转发',
        path: '/pages/awardconfirm/awardconfirm?awardid=' + this.data.awardid,
        success: function (res) {
          console.log('成功', res)
        }
      }
    } else wx.showModal({
      title: '请稍后重试',
      content: '网络出现异常请稍后重试',
    })
  },


  go_to_advice: function () {
    wx.navigateTo({
      url: '/pages/advice/advice',
    })
  },

  oook:function(){
    wx.navigateTo({
      url: '/pages/look_lucky_user/look_lucky_user?awardid='+this.data.awardid,
    })
  }

})